/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaclientcp3110.UCM;

import common.UCM.UCController;
import javaclientcp3110.Model.MainModel;
import javaclientcp3110.Model.Product.ProductDeleteModel;
import javaclientcp3110.View.Product.ProductDeleteView;

/**
 *UC Delete Product
 *
 * Pre-condition: A valid user is logged into the system.
 *
 *  1. User selects to delete a product
 *  2. System displays a list of products
 *  3. User select product and clicks delete
 *  4. System deletes product from the storage medium
 *
 * Post-condition: The product is deleted from the system
 *
 * Unsuccessful Outcomes:
 *
 *   * 4a) System is unable to update the storage medium
 *        1. System displays an error message
 */
public class UCDeleteProduct implements UCController{
    private ProductDeleteView view;
    private ProductDeleteModel model;

    public UCDeleteProduct() {
        this(new ProductDeleteView(),new ProductDeleteModel());
    }

    public UCDeleteProduct(ProductDeleteView view, ProductDeleteModel model) {
        this.view = view;
        this.model = model;
    }

    public void run() {
    //*  1. User selects to delete a product
    //*  2. System displays a list of products
    //*  3. User select product and clicks delete
    //*  4. System deletes product from the storage medium
        model.setDeleteProduct(view.getProduct());
        boolean deleted = model.deleteProduct();
        if (deleted) {
            view.successMessage("Product successfully deleted!");
            MainModel.getInstance().updateData();
            view.updateProductList();
        } else {
    //*   * 4a) System is unable to update the storage medium
    //*        1. System displays an error message
            view.errorMessage("The product could not be deleted");
        }
    }
}
